<template>
    <div>
        <el-tabs type="border-card" v-model="tabType" @tabClick="tabClick">
            <el-tab-pane label="应急物资储备" name="0">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5" v-if="parentBtnType !== 'check'">
                        <el-button type="primary" icon="Plus" @click="materialHandleAdd">添加</el-button>
                        <el-button plain icon="Download" @click="handleImport">导入</el-button>
                        <el-button plain @click="importTemplate" class="fr">模板下载</el-button>
                    </el-col>
                    <p></p>
                    <el-table class="mt15" v-loading="loading" :data="materialDataList" stripe>
                        <el-table-column label="名称" align="center" prop="scSuppliesName" />
                        <el-table-column label="规格型号" align="center" prop="scSuppliesBrand" />
                        <el-table-column label="数量" align="center" prop="scSuppliesNumber" />
                        <el-table-column label="单位" align="center" prop="scSuppliesUnit" />
                        <el-table-column label="备注" align="center" prop="scSuppliesNote" />
                        <el-table-column label="操作" v-if="parentBtnType !== 'check'" align="center"
                            class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <!-- 分页 -->
                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page"
                        v-model:limit="queryParams.rows" @pagination="getTableData" />
                </el-row>

            </el-tab-pane>
            <el-tab-pane label="应急救援力量" name="1">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5" v-if="parentBtnType !== 'check'">
                        <el-button type="primary" icon="Plus" @click="rescueHandleAdd">添加</el-button>
                        <el-button plain icon="Download" @click="handleImport">导入</el-button>
                        <el-button plain @click="importTemplate" class="fr">模板下载</el-button>
                    </el-col>
                    <p></p>
                    <el-table class="mt15" v-loading="loading" :data="rescueDataList" stripe>
                        <el-table-column label="队伍名称" align="center" prop="scRescueName" />
                        <el-table-column label="所属行业" align="center" prop="scRescueIndustry" />
                        <el-table-column label="隶属单位" align="center" prop="scRescueUnit" />
                        <el-table-column label="地址" align="center" prop="scRescueAddress" />
                        <el-table-column label="队伍总人数" align="center" prop="scRescueNumber" />
                        <el-table-column label="备注" align="center" prop="scRescueNote" />
                        <el-table-column label="操作" v-if="parentBtnType !== 'check'" align="center"
                            class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page"
                        v-model:limit="queryParams.rows" @pagination="getTableData" />
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="应急装备体系" name="2">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5" v-if="parentBtnType !== 'check'">
                        <el-button type="primary" icon="Plus" @click="equipTitleHandleAdd">添加</el-button>
                        <el-button plain icon="Download" @click="handleImport">导入</el-button>
                        <el-button plain @click="importTemplate" class="fr">模板下载</el-button>
                    </el-col>
                    <p></p>
                    <el-table class="mt15" v-loading="loading" :data="equipDataList" stripe>
                        <el-table-column label="装备名称" align="center" prop="scEquipName" />
                        <el-table-column label="数量（台）" align="center" prop="scEquipNumber" />
                        <el-table-column label="备注" align="center" prop="scEquipNote" />
                        <el-table-column label="操作" v-if="parentBtnType !== 'check'" align="center"
                            class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page"
                        v-model:limit="queryParams.rows" @pagination="getTableData" />
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="避难场所建设" name="3">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5" v-if="parentBtnType !== 'check'">
                        <el-button type="primary" icon="Plus" @click="placeHandleAdd">添加</el-button>
                        <el-button plain icon="Download" @click="handleImport">导入</el-button>
                        <el-button plain @click="importTemplate" class="fr">模板下载</el-button>
                    </el-col>
                    <p></p>
                    <el-table class="mt15" v-loading="loading" :data="placeDataList" stripe>
                        <el-table-column label="个数" align="center" prop="scSanctuaryNumber" />
                        <el-table-column label="面积（m2）" align="center" prop="scSanctuaryArea" />
                        <el-table-column label="容纳人数" align="center" prop="scSanctuaryNumberpeople" />
                        <el-table-column label="操作" v-if="parentBtnType !== 'check'" align="center"
                            class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page"
                        v-model:limit="queryParams.rows" @pagination="getTableData" />
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="医疗力量" name="4">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5" v-if="parentBtnType !== 'check'">
                        <el-button type="primary" icon="Plus" @click="healthHandleAdd">添加</el-button>
                        <el-button plain icon="Download" @click="handleImport">导入</el-button>
                        <el-button plain @click="importTemplate" class="fr">模板下载</el-button>
                    </el-col>
                    <p></p>
                    <el-table class="mt15" v-loading="loading" :data="healthDataList" stripe>
                        <el-table-column label="单位名称" align="center" prop="scMedicalName" />
                        <el-table-column label="医生人数" align="center" prop="scMedicalDoctor" />
                        <el-table-column label="护士人数" align="center" prop="scMedicalNurse" />
                        <el-table-column label="备注" align="center" prop="scMedicalNote" />
                        <el-table-column label="操作" v-if="parentBtnType !== 'check'" align="center"
                            class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button type="text" @click="handleUpdate(scope.row)">修改</el-button>
                                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page"
                        v-model:limit="queryParams.rows" @pagination="getTableData" />
                </el-row>
            </el-tab-pane>
        </el-tabs>
    </div>
    <!-- 应急物资弹窗 -->
    <el-dialog :title="materialTitle" v-model="materialShowPopup" width="600px" append-to-body>
        <el-form ref="materialFormRef" :model="materialForm" :rules="materialRules" label-width="120px">
            <el-form-item class="mt15" label="名称" prop="scSuppliesName">
                <el-input placeholder="请输入名称" v-model="materialForm.scSuppliesName" />
            </el-form-item>
            <el-form-item class="mt15" label="规格型号/品牌" prop="scSuppliesBrand">
                <el-input placeholder="请输入规格型号/品牌" v-model="materialForm.scSuppliesBrand" />
            </el-form-item>
            <el-form-item class="mt15" label="数量" prop="scSuppliesNumber">
                <el-input placeholder="请输入数量" v-model.number="materialForm.scSuppliesNumber" />
            </el-form-item>
            <el-form-item class="mt15" label="单位" prop="scSuppliesUnit">
                <el-input placeholder="请输入单位" v-model="materialForm.scSuppliesUnit" />
            </el-form-item>
            <el-form-item class="mt15" label="备注" prop="scSuppliesNote">
                <el-input type="textarea" v-model="materialForm.scSuppliesNote" maxlength="500" show-word-limit
                    :autosize="{ minRows: 4, maxRows: 5}" placeholder="请输入备注" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button plain @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 应急救援弹窗 -->
    <el-dialog :title="rescueTitle" v-model="rescueShowPopup" width="600px" append-to-body>
        <el-form ref="rescueFormRef" :model="rescueForm" :rules="rescueRules" label-width="120px">
            <el-form-item class="mt15" label="队伍名称" prop="scRescueName">
                <el-input placeholder="请输入队伍名称" v-model="rescueForm.scRescueName" />
            </el-form-item>
            <el-form-item class="mt15" label="所属行业" prop="scRescueIndustry">
                <el-input placeholder="请输入所属行业" v-model="rescueForm.scRescueIndustry" />
            </el-form-item>
            <el-form-item class="mt15" label="隶属单位" prop="scRescueUnit">
                <el-input placeholder="请输入隶属单位" v-model="rescueForm.scRescueUnit" />
            </el-form-item>
            <el-form-item class="mt15" label="地址" prop="scRescueAddress">
                <el-input placeholder="请输入地址" v-model="rescueForm.scRescueAddress" />
            </el-form-item>
            <el-form-item class="mt15" label="单位总人数" prop="scRescueNumber">
                <el-input placeholder="请输入单位总人数" v-model.number="rescueForm.scRescueNumber" />
            </el-form-item>
            <el-form-item class="mt15" label="备注" prop="scRescueNote">
                <el-input type="textarea" v-model="rescueForm.scRescueNote" maxlength="500" show-word-limit
                    :autosize="{ minRows: 4, maxRows: 5}" placeholder="请输入备注" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button plain @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 应急装备弹框 -->
    <el-dialog :title="equipTitle" v-model="equipShowPopup" width="600px" append-to-body>
        <el-form ref="equipFormRef" :model="equipForm" :rules="equipRules" label-width="120px">
            <el-form-item class="mt15" label="装备名称" prop="scEquipName">
                <el-input placeholder="请输入装备名称" v-model="equipForm.scEquipName" />
            </el-form-item>
            <el-form-item class="mt15" label="数量（台）" prop="scEquipNumber">
                <el-input placeholder="请输入数量（台）" v-model.number="equipForm.scEquipNumber" />
            </el-form-item>
            <el-form-item class="mt15" label="备注" prop="scEquipNote">
                <el-input type="textarea" maxlength="500" show-word-limit :autosize="{ minRows: 4, maxRows: 5}"
                    placeholder="请输入备注" v-model="equipForm.scEquipNote" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button plain @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 避难避难场所建设弹框 -->
    <el-dialog :title="placeTitle" v-model="placeShowPopup" width="600px" append-to-body>
        <el-form ref="placeFormRef" :model="placeForm" :rules="placeRules" label-width="120px">
            <el-form-item class="mt15" label="个数" prop="scSanctuaryNumber">
                <el-input placeholder="请输入个数" v-model.number="placeForm.scSanctuaryNumber" />
            </el-form-item>
            <el-form-item class="mt15" label="面积（m2）" prop="scSanctuaryArea">
                <el-input placeholder="面积（m2）" v-model="placeForm.scSanctuaryArea" />
            </el-form-item>
            <el-form-item class="mt15" label="容纳人数" prop="scSanctuaryNumberpeople">
                <el-input placeholder="请输入数量" v-model.number="placeForm.scSanctuaryNumberpeople" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button plain @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 医疗力量弹框 -->
    <el-dialog :title="healthTitle" v-model="healthShowPopup" width="600px" append-to-body>
        <el-form ref="healthFormRef" :model="healthForm" :rules="healthRules" label-width="120px">
            <el-form-item class="mt15" label="单位名称" prop="scMedicalName">
                <el-input placeholder="请输入单位名称" v-model="healthForm.scMedicalName" />
            </el-form-item>
            <el-form-item class="mt15" label="医生人数" prop="scMedicalDoctor">
                <el-input placeholder="请输入医生人数" v-model.number="healthForm.scMedicalDoctor" />
            </el-form-item>
            <el-form-item class="mt15" label="护士人数" prop="scMedicalNurse">
                <el-input placeholder="请输入护士人数" v-model.number="healthForm.scMedicalNurse" />
            </el-form-item>
            <el-form-item class="mt15" label="备注" prop="scMedicalNote">
                <el-input type="textarea" maxlength="500" show-word-limit :autosize="{ minRows: 4, maxRows: 5}"
                    placeholder="请输入备注" v-model="healthForm.scMedicalNote" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button plain @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
        <p></p>
        <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
            :action="parentBtnType == 'add' ? upload.addUrl: upload.url" :disabled="upload.isUploading"
            :before-upload="handleFileBeforeUpload" :on-progress="handleFileUploadProgress"
            :on-error="handleFileUploadError" :on-success="handleFileSuccess" :auto-upload="false" drag>
            <el-icon class="el-icon--upload">
                <upload-filled />
            </el-icon>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <template #tip>
                <div class="el-upload__tip text-center">
                    <span>仅允许导入xls、xlsx格式文件。</span>
                </div>
            </template>
        </el-upload>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button plain @click="upload.open = false">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { getDetails, getDetail, updateScSupplies, saveScSupplies, delSeismicSetout, saveScRescue, updateScRescue, saveScEquip, updateScEquip, saveScSanctuary, updateScSanctuary, saveScMedical, updateScMedical, saveSeismicSetout1 } from "@/api/basedatamanage/emergencyPreparedness.js";
import { getToken } from "@/utils/auth";
const { proxy } = getCurrentInstance();
const props = defineProps({
    id: {
        default: null,
        type: String,
    },
    parentBtnType: {
        default: null,
        type: String,
    },
    form: {
        default: () => ({}),
        type: Object,
    }
})
const { id, parentBtnType, form } = toRefs(props);
const materialDataList = ref([]);  //应急物资列表
const rescueDataList = ref([]);  //应急救援列表
const equipDataList = ref([]);  //应急装备列表
const placeDataList = ref([]);  //避难救援场所列表
const healthDataList = ref([]);  //医疗力量列表
let loading = ref(false);   //加载状态
let total = ref(0);   //列表总条数
let tabType = ref('0');
let materialShowPopup = ref(false); //应急物资弹框
let materialTitle = ref('');
let rescueShowPopup = ref(false); //应急救援弹框
let rescueTitle = ref('');
let equipShowPopup = ref(false); //应急装备弹框
let equipTitle = ref('');
let placeShowPopup = ref(false); //避难救援场所弹框
let placeTitle = ref('');
let healthShowPopup = ref(false); //医疗力量弹框
let healthTitle = ref('');
let isEdit = ref(false);
let prefectureId = ref('');
let cityId = ref('');
let provinceId = ref('');

//导入
const upload = reactive({
    open: false,
    title: "",
    isUploading: false,
    headers: { token: "tokrn " + getToken() },
    url: `${import.meta.env.VITE_APP_BASE_API}/fxfzypg/baseData/importExcel?scId=${id.value}&type=${parseInt(tabType.value) + 1}`,
    addUrl: `${import.meta.env.VITE_APP_BASE_API}/fxfzypg/baseData/saveSeismicSetout2?type=${parseInt(tabType.value) + 1}&provinceId=${provinceId.value}&cityId=${cityId.value}&prefectureId=${prefectureId.value}`,
});
const queryParams = reactive({
    page: 1,
    rows: 10,
    id: props.id,
    type: 1,
})

const materialData = reactive({
    materialForm: {},
    materialRules: {
        scSuppliesName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        scSuppliesBrand: [{ required: true, message: "规格型号/品牌不能为空", trigger: "blur" }],
        scSuppliesNumber: [{ required: true, message: "数量不能为空", trigger: "blur" }, { type: 'number', message: '数量必须为整数', trigger: "blur" }],
        scSuppliesUnit: [{ required: true, message: "单位不能为空", trigger: "blur" }],
    }
})
const rescueData = reactive({
    rescueForm: {},
    rescueRules: {
        scRescueName: [{ required: true, message: "队伍名称不能为空", trigger: "blur" }],
        scRescueIndustry: [{ required: true, message: "所属行业不能为空", trigger: "blur" }],
        scRescueUnit: [{ required: true, message: "隶属单位不能为空", trigger: "blur" }],
        scRescueAddress: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        scRescueNumber: [{ required: true, message: "单位总人数不能为空", trigger: "blur" }, { type: 'number', message: '单位总人数必须为整数', trigger: "blur" }],
    }
})
const equipData = reactive({
    equipForm: {},
    equipRules: {
        scEquipName: [{ required: true, message: "装备名称不能为空", trigger: "blur" }],
        scEquipNumber: [{ required: true, message: "数量不能为空", trigger: "blur" }, { type: 'number', message: '数量必须为整数', trigger: "blur" }],
    }
})
const placeData = reactive({
    placeForm: {},
    placeRules: {
        scSanctuaryNumber: [{ required: true, message: "个数不能为空", trigger: "blur" }, { type: 'number', message: '个数必须为整数', trigger: "blur" }],
        scSanctuaryArea: [{ required: true, message: "面积不能为空", trigger: "blur" }, { message: '容纳人数必须为数字，且为两位小数', trigger: "blur", pattern: /^(([1-9]+\d*)|(0{1}))(.[0-9]{2})?$/ }],
        scSanctuaryNumberpeople: [{ required: true, message: "容纳人数不能为空", trigger: "blur" }, { type: 'number', message: '容纳人数必须为整数', trigger: "blur" }],
    }
})
const healthData = reactive({
    healthForm: {},
    healthRules: {
        scMedicalName: [{ required: true, message: "单位名称不能为空", trigger: "blur" }],
        scMedicalDoctor: [{ required: true, message: "医生人数不能为空", trigger: "blur" }, { type: 'number', message: '医生人数必须为整数', trigger: "blur" }],
        scMedicalNurse: [{ required: true, message: "护士人数不能为空", trigger: "blur" }, { type: 'number', message: '护士人数必须为整数', trigger: "blur" }],
    }
})

const { materialForm, materialRules } = toRefs(materialData);
const { rescueForm, rescueRules } = toRefs(rescueData);
const { equipForm, equipRules } = toRefs(equipData);
const { placeForm, placeRules } = toRefs(placeData);
const { healthForm, healthRules } = toRefs(healthData);

function setURL() {
    upload.url = `${import.meta.env.VITE_APP_BASE_API}/fxfzypg/baseData/importExcel?scId=${id.value}&type=${parseInt(tabType.value) + 1}`
    upload.addUrl = `${import.meta.env.VITE_APP_BASE_API}/fxfzypg/baseData/saveSeismicSetout2?type=${parseInt(tabType.value) + 1}&provinceId=${form.value.provinceId}&cityId=${form.value.cityId}&prefectureId=${form.value.prefectureId}`
}

//获取列表信息
function getTableData() {
    loading.value = true;
    getDetails({...queryParams, id: props.id}).then(response => {
        
        if (tabType.value == '0') materialDataList.value = response.data.rows;
        if (tabType.value == '1') rescueDataList.value = response.data.rows;
        if (tabType.value == '2') equipDataList.value = response.data.rows;
        if (tabType.value == '3') placeDataList.value = response.data.rows;
        if (tabType.value == '4') healthDataList.value = response.data.rows;
        console.log(tabType, materialDataList)
        total.value = response.data.total;
        loading.value = false;
    });
}
//切换tab
function tabClick(val) {
    queryParams.type = parseInt(val.index) + 1;
    getTableData();
}

/** 修改按钮操作 */
function handleUpdate(row) {
    // btnType.value = 'edit';
    let id = "";
    
    isEdit.value = true;
    if (tabType.value == 0) id = row.suId || '';
    if (tabType.value == 1) id = row.reId || '';
    if (tabType.value == 2) id = row.eqId || '';
    if (tabType.value == 3) id = row.saId || '';
    if (tabType.value == 4) id = row.meId || '';
    getDetail({ id: id, type: parseInt(tabType.value) + 1 }).then(response => {
        if (tabType.value == 0) {
            materialForm.value = { ...response.data };
            materialForm.value.id = response.data.suId;
            materialForm.value.scSuppliesNumber = Number(response.data.scSuppliesNumber);
            materialShowPopup.value = true;
            materialTitle.value = "修改应急物资";
        }
        if (tabType.value == 1) {
            rescueForm.value = { ...response.data };
            rescueForm.value.id = response.data.reId;
            rescueForm.value.scRescueNumber = Number(response.data.scRescueNumber);
            rescueShowPopup.value = true;
            rescueTitle.value = "修改应急救援力量";
        }
        if (tabType.value == 2) {
            equipForm.value = { ...response.data };
            equipForm.value.id = response.data.eqId;
            equipForm.value.scEquipNumber = Number(response.data.scEquipNumber);
            equipShowPopup.value = true;
            equipTitle.value = "修改应急装备体系";
        }
        if (tabType.value == 3) {
            placeForm.value = { ...response.data };
            placeForm.value.id = response.data.saId;
            placeForm.value.scSanctuaryNumberpeople = Number(response.data.scSanctuaryNumberpeople);
            placeShowPopup.value = true;
            placeTitle.value = "修改避难场所建设";
        }
        if (tabType.value == 4) {
            healthForm.value = { ...response.data };
            healthForm.value.id = response.data.meId;
            healthForm.value.scMedicalDoctor = Number(response.data.scMedicalDoctor);
            healthForm.value.scMedicalNurse = Number(response.data.scMedicalNurse);
            healthShowPopup.value = true;
            healthTitle.value = "修改医疗力量";
        }

    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    let id = "";
    if (tabType.value == 0) id = row.suId || '';
    if (tabType.value == 1) id = row.reId || '';
    if (tabType.value == 2) id = row.eqId || '';
    if (tabType.value == 3) id = row.saId || '';
    if (tabType.value == 4) id = row.meId || '';
    proxy.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function () {
        return delSeismicSetout(id, parseInt(tabType.value) + 1);
    }).then(() => {
        getTableData();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

// 重置表单
function reset() {
    materialForm.value = {};
    rescueForm.value = {};
    equipForm.value = {};
    placeForm.value = {};
    healthForm.value = {};

    proxy.resetForm("materialFormRef");
    proxy.resetForm("rescueFormRef");
    proxy.resetForm("equipFormRef");
    proxy.resetForm("placeFormRef");
    proxy.resetForm("healthFormRef");
}

/** 取消按钮 */
function cancel() {
    materialShowPopup.value = false;
    rescueShowPopup.value = false;
    equipShowPopup.value = false;
    placeShowPopup.value = false;
    healthShowPopup.value = false;
    reset();
};

//提交表单
function submitForm() {
    let updataApi = null;
    let saveApi = null;
    let formRef = '';
    let scId = null;
    let dataName = {};

    if (parentBtnType.value == 'add' && isEdit.value === false) {
        if (tabType.value == 0) {
            let obj = {
                scSeismicSetout: { ...form.value },
                scSupplies: { ...materialForm.value }
            };
            addSave('materialFormRef', obj);
        }
        if (tabType.value == 1) {
            let obj = {
                scSeismicSetout: { ...form.value },
                scRescue: { ...rescueForm.value }
            }
            addSave('rescueFormRef', obj)
        }
        if (tabType.value == 2) {
            let obj = {
                scSeismicSetout: { ...form.value },
                scEquip: { ...equipForm.value }
            }
            addSave('equipFormRef', obj)
        }
        if (tabType.value == 3) {
            let obj = {
                scSeismicSetout: { ...form.value },
                scSanctuary: { ...placeForm.value }
            }
            addSave('placeFormRef', obj)
        }
        if (tabType.value == 4) {
            let obj = {
                scSeismicSetout: { ...form.value },
                scMedical: { ...healthForm.value }
            }
            addSave('healthFormRef', obj)
        }
    } else {
        if (tabType.value == 0) {
            updataApi = updateScSupplies;
            saveApi = saveScSupplies;
            formRef = 'materialFormRef';
            materialForm.value.scId = id.value;
            materialForm.value.type = 1;
            scId = materialForm.value.id || null;
            dataName = materialForm.value;
        }
        if (tabType.value == 1) {
            updataApi = updateScRescue;
            saveApi = saveScRescue;
            formRef = 'rescueFormRef';
            rescueForm.value.scId = id.value;
            rescueForm.value.type = 2;
            scId = rescueForm.value.id || null;
            dataName = rescueForm.value;
        }
        if (tabType.value == 2) {
            updataApi = updateScEquip;
            saveApi = saveScEquip;
            formRef = 'equipFormRef';
            equipForm.value.scId = id.value;
            equipForm.value.type = 3;
            scId = equipForm.value.id || null;
            dataName = equipForm.value;
        }
        if (tabType.value == 3) {
            updataApi = updateScSanctuary;
            saveApi = saveScSanctuary;
            formRef = 'placeFormRef';
            placeForm.value.scId = id.value;
            placeForm.value.type = 4;
            scId = placeForm.value.id || null;
            dataName = placeForm.value;
        }
        if (tabType.value == 4) {
            updataApi = updateScMedical;
            saveApi = saveScMedical;
            formRef = 'healthFormRef';
            healthForm.value.scId = id.value;
            healthForm.value.type = 5;
            scId = healthForm.value.id || null;
            dataName = healthForm.value;
        }
    }

    proxy.$refs[formRef]?.validate(valid => {
        if (valid) {
            if (scId != null) {
                updataApi(dataName).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    handleResult();

                });
            } else {
                saveApi(dataName).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    handleResult();
                });
            }
            scId = null;
            dataName = {};
            reset();
        }
    });
}

function addSave(formRef, obj) {
    proxy.$refs[formRef].validate(valid => {
        if (valid) {
            saveSeismicSetout1(obj).then(response => {
                proxy.$modal.msgSuccess("新增成功");
                // reset();
                id.value = response.data.scSeismicSetout.scId;
                queryParams.id = response.data.scSeismicSetout.scId;
                handleResult();
            });
        }
    });
}

/** 下载模板操作 */
function importTemplate() {
    let str = "";
    if (tabType.value == 0) str = "应急准备情况模板-应急物资储备.xlsx";
    if (tabType.value == 1) str = "应急准备情况模板-应急救援力量.xlsx";
    if (tabType.value == 2) str = "应急准备情况模板-应急装备体系.xlsx";
    if (tabType.value == 3) str = "应急准备情况模板-避难场所建设.xlsx";
    if (tabType.value == 4) str = "应急准备情况模板-医疗力量.xlsx";
    proxy.download("/base/product/downloadExcel", {
        fileName: str
    }, str);
};

/** 提交上传文件 */
function submitFileForm() {
    const i = proxy.$refs["uploadRef"].submit();
    getTableData();
};

/** 打开导入表弹窗 */
function handleImport() {
    setURL();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }

    upload.title = "导入";
    upload.open = true;
}
/**文件上传前处理 */
function handleFileBeforeUpload(file) {
    proxy.$modal.loading("正在上传文件，请稍候...");
    return true
}

/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
    upload.isUploading = true;
};
// 上传失败
function handleFileUploadError(err) {
    proxy.$modal.closeLoading();
    proxy.$modal.msgError("上传文件失败");
}
/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
    upload.open = false;
    upload.isUploading = false;
    proxy.$modal.closeLoading();
    proxy.$refs["uploadRef"].handleRemove(file);
    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.message + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
    id.value = response.data.scId;
    queryParams.id = response.data.scId;
    handleResult();
};
//处理成功弹窗
function handleResult() {
    if (tabType.value == 0) materialShowPopup.value = false;
    if (tabType.value == 1) rescueShowPopup.value = false;
    if (tabType.value == 2) equipShowPopup.value = false;
    if (tabType.value == 3) placeShowPopup.value = false;
    if (tabType.value == 4) healthShowPopup.value = false;
    getTableData();
}
//新增应急物资弹窗
function materialHandleAdd() {
    reset();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }
    materialShowPopup.value = true;
    materialTitle.value = '新增应急物资'
}

//新增应急救援弹框
function rescueHandleAdd() {
    reset();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }
    rescueShowPopup.value = true;
    rescueTitle.value = '新增应急救援'
}

//新增应急装备弹框
function equipTitleHandleAdd() {
    reset();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }
    equipShowPopup.value = true;
    equipTitle.value = '新增应急装备'
}

//新增避难救避难场所建设弹窗
function placeHandleAdd() {
    reset();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }
    placeShowPopup.value = true;
    placeTitle.value = '新增避难场所建设'
}

//新增医疗力量弹窗
function healthHandleAdd() {
    reset();
    if (!validatorForm()) {
        return proxy.$modal.msgWarning("请选择省市区后再进行添加")
    }
    healthShowPopup.value = true;
    healthTitle.value = '新增医疗力量'
}
function validatorForm() {
    isEdit.value = false;
    if (!form.value.provinceId || !form.value.cityId || !form.value.prefectureId) {
        return false;
    }
    return true;
}

function passFather() {
    return {
        id: queryParams.id || props.id, //应急物资列表
    };
}

defineExpose({ passFather })

watch(() => props.id, () => {
    if(props.id){
        getTableData();
    }
}, {immediate: true})
</script>

<style lang="scss" scoped>
.mt15 {
    margin-top: 15px;
}

.fr {
    float: right !important;
    // flex: 1;text-align: right;
    // margin-left: auto;
    // margin-left: auto
}
</style>